<template>
  <div class="musicList">
    <!-- 文字标题 -->
    <div class="musicTop">
      <div class="title">发现好歌单</div>
      <div class="more">查看更多</div>
    </div>
    <!-- 轮播图区域 -->
    <div class="musicSwiper">
      <van-swipe
        :loop="false"
        :width="130"
        class="my-swiper"
        :show-indicators="false"
      >
        <van-swipe-item v-for="item in state.musicList" :key="item.id">
          <router-link :to="{path:'/itemMusic',query:{id:item.id}}">
          <img :src="item.picUrl" alt="" />
          <span class="playCount">
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-gl-play"></use>
            </svg>
            {{ changeCount(item.playCount) }}
          </span>
          <span class="name">{{ item.name }}</span>
          </router-link>
        </van-swipe-item>
      </van-swipe>
    </div>
  </div>
</template>

<script>
import { getMusicList } from "@/api/home.js";
import { reactive, onMounted } from "vue";
export default {
  // vue2写法`
    // data() {
    //   return {
    //     musicList: [],
    //   };
    // },
    // methods: {
    //   // 获取歌单数据
    //   async getGedan() {
    //     const { data: res } = await getMusicList();
    //     console.log(res);
    //     this.musicList = res.result;
    //   },
    //   // 歌单点击量处理
    //   changeCount(num){
    //     if(num>=100000000){
    //         return (num/100000000).toFixed(1)+'亿'
    //     }else if(num>=10000){
    //         return  (num/10000).toFixed(1)+'万'
    //     }
    //   }
    // },
    // mounted() {
    //   this.getGedan();
    // },
  // vue3写法
  setup(){
    const state=reactive({
      musicList:[]
    });
      // 歌单点击量处理
    function  changeCount(num){
        if(num>=100000000){
            return (num/100000000).toFixed(1)+'亿';
        }else if(num>=10000){
            return  (num/10000).toFixed(1)+'万';
        }
      };
    onMounted(async()=>{
        const {data:res}=await getMusicList()
        // console.log(res);
        state.musicList=res.result
    })
    return {state,changeCount}
  }
};
</script>

<style lang="less" scoped>
.musicList {
  width: 100%;
  padding: 0.2rem;
  height: 5rem;
  .musicTop {
    height: 0.6rem;
    width: 100%;
    display: flex;
    justify-content: space-between;
    margin-bottom: 0.2rem;
    .title {
      font-size: 0.4rem;
      font-weight: 900;
    }
    .more {
      border: 0.02rem solid #ccc;
      text-align: center;
      line-height: 0.6rem;
      padding: 0 0.2rem;
      border-radius: 0.4rem;
    }
  }
  .musicSwiper {
    width: 100%;
    height: 3.8rem;
    .my-swiper {
      height: 100%;
      .van-swipe-item {
        padding-right: 0.2rem;
        position: relative;
        img {
          height: 2.6rem;
          width: 100%;
          border-radius: 0.1rem;
        }
        .playCount {
          position: absolute;
          z-index: 20;
          right: 0.3rem;
          top: 0.1rem;
          color: white;
          font-size: 0.16rem;
          font-weight: 800;
          .icon {
            width: 0.3rem;
            height: 0.3rem;
          }
        }
        .name {
          font-size: 0.28rem;
          font-weight: 400;
          overflow: hidden;
          text-overflow: ellipsis;
          color:black ;
        }
      }
    }
  }
}
</style>